﻿using Microsoft.EntityFrameworkCore;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.ProjectManager;
using SJ.Platform.Extend.ModelExtend.ProjectExt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Project
{
    public class Project_Schedule_Task_DoHandle
    {
        private readonly SJDBContext _context;

        public Project_Schedule_Task_DoHandle(SJDBContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> CreateAsync(ProjectScheduleTaskDo entity)
        {
            try
            {
                _context.ProjectScheduleTaskDos.Add(entity);
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>Create", ex);
                return false;
            }
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> BatchCreateAsync(List<ProjectScheduleTaskDo> list)
        {
            try
            {
                int result = 0;
                if (list != null && list.Count > 0)
                {
                    foreach (ProjectScheduleTaskDo entity in list)
                    {
                        _context.ProjectScheduleTaskDos.Add(entity);
                    }
                    result = await _context.SaveChangesAsync();
                }

                return result == list.Count;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>BatchCreateAsync", ex);
                return false;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(ProjectScheduleTaskDo entity)
        {
            try
            {
                _context.Entry(entity).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>Update", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(ProjectScheduleTaskDo entity)
        {
            try
            {
                _context.ProjectScheduleTaskDos.Remove(entity);
                return _context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>Delete", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdAsync(string id)
        {
            try
            {
                var entity = _context.ProjectScheduleTaskDos.Find(id);
                if (entity != null)
                {
                    _context.ProjectScheduleTaskDos.Remove(entity);
                    await _context.SaveChangesAsync();
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>DeleteById", ex);
            }
            return false;
        }

        /// <summary>
        /// 根据任务id删除
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByTaskIdAsync(string taskId)
        {
            try
            {
                var taskdos = await _context.ProjectScheduleTaskDos.Where(d => d.TaskId == taskId).ToListAsync();
                int count = _context.Database.ExecuteSqlRaw($"delete from project_schedule_task_do where TaskId='{taskId}';");
                return taskdos.Count == count;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>DeleteByTaskIdAsync", ex);
            }
            return false;
        }

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProjectScheduleTaskDo> GetEntityByIdAsync(string id)
        {
            try
            {
                return await _context.ProjectScheduleTaskDos.FindAsync(id);
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>GetEntityById", ex);
            }
            return null;
        }

        /// <summary>
        /// 根据任务ID获取完成记录
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public async Task<List<ProjectScheduleTaskDoExtend>> GetTaskDosByTaskIdAsync(string taskId)
        {
            try
            {
                //任务完成记录
                var query = from user in _context.SysUsers
                            join taskdo in _context.ProjectScheduleTaskDos on user.Id equals taskdo.AddUserId
                            where taskdo.TaskId == taskId
                            select new ProjectScheduleTaskDoExtend
                            {
                                Id = taskdo.Id,
                                TaskId = taskdo.TaskId,
                                Mark = taskdo.Mark,
                                Progress = taskdo.Progress,
                                AddTime = taskdo.AddTime,
                                AddUserId = taskdo.AddUserId,
                                EditTime = taskdo.EditTime,
                                EditUserId = taskdo.EditUserId,
                                AddUserName = user.RealName
                            };

                return await query.ToListAsync(); ;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Schedule_Task_DoHandle=>GetTaskDosByTaskIdAsync", ex);
                return null;
            }
        }
    }
}
